package com.compton.UI
{
	import com.compton.EVENT.WineEvent;
	import com.compton.VO.SearchVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import libs.Amount_Scroll;
	import libs.SearchPanelBase;
	import libs.SliderAssets;
	import libs.TypeMask;
	
	public class Search extends SearchPanelBase
	{
		private var _sVO:SearchVO;
		
		private var _whiteWines:Array = ["Chardonnay","Riesling", "Zinfandel", "Pinot Grigio","Cabernet Sauvignon", "Pinot Noir", "Syrah", "Malbec", "Merlot", "Tempranillo"];
		private var _places:Array = ["US", "AU", "FR", "UK", "GB", "TI", "PT", "CH", "CA", "NZ", "ES"];  
		
		private var _whiteWine:WhiteBtn;
		private var _btnArray:Array = [];
		private var _type:String = "Chardonnay";
		private var _sliderBox1:SliderAssets;
		private var _sliderBox2:SliderAssets;
		private var _amntSlider1:Amount_Scroll;
		private var _amntSlider2:Amount_Scroll;
		private var _slider1:SliderManager;
		private var _slider2:SliderManager;
		private var _placeSlider1:SliderManager;
		private var _placeSlider2:SliderManager;
		private var _lb:LayoutBox;
		private var _typePercent:Number;
		private var _mask:TypeMask;
		private var _placesMask:TypeMask;
		private var _placesLb:LayoutBox;
		private var _placeed:Number;
		private var _place:String = "US";
		private var _maxPrice:Number = 0;
		private var _minPrice:Number = 0;
		private var _searchResults:SearchVO;
		
		public function Search(searchResults:SearchVO = null)
		{
			super();
			_searchResults = searchResults;
			assemble();
		}
		
		//Starts assembling the look and feel of the search view. Sliders, buttons, layoutboxes, and masks
		private function assemble():void
		{
			SearchBtn.addEventListener(MouseEvent.CLICK, onClick);
			SearchBtn.buttonMode = true;
			
			_lb = new LayoutBox(10,true);
			_lb.x = 33;
			_lb.y = 120;
			this.addChild(_lb);
			
			_placesLb = new LayoutBox(10, true);
			_placesLb.x = 33;
			_placesLb.y = 225;
			this.addChild(_placesLb);
			
			_mask = new TypeMask();
			_mask.x = 20;
			_mask.y = 120;
			_lb.mask = _mask;
			
			_placesMask = new TypeMask();
			_placesMask.x = 22;
			_placesMask.y = 222;
			_placesLb.mask = _placesMask;
			
			tf_maxPrice.text = "$" + "0";
			
			this.Background.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			createButtonGroup();
			addSlider();
		}
		
		private function onDown(evt:MouseEvent):void
		{
			stage.nativeWindow.startMove();
		}
		
		//adds the sliders to the stage and sets up their event listeners
		private function addSlider():void
		{
			_sliderBox1 = new SliderAssets();
			_sliderBox1.x = 23;
			_sliderBox1.y = 175;
			this.addChild(_sliderBox1);
			
			_sliderBox2 = new SliderAssets();
			_sliderBox2.x = _sliderBox1.x;
			_sliderBox2.y = 280;
			this.addChild(_sliderBox2);
			
			_amntSlider1 = new Amount_Scroll();
			_amntSlider1.x = 23;
			_amntSlider1.y = 325;
			this.addChild(_amntSlider1);
			
			_amntSlider2 = new Amount_Scroll();
			_amntSlider2.x = _amntSlider1.x;
			_amntSlider2.y = 382;
			this.addChild(_amntSlider2);
			
			_slider1 = new SliderManager();
			_slider1.setUpAsstes(_sliderBox1.track, _sliderBox1.handle);
			_sliderBox1.handle.buttonMode = true;
			_slider1.addEventListener(Event.CHANGE, onChange);
			
			_slider2 = new SliderManager();
			_slider2.setUpAsstes(_sliderBox2.track, _sliderBox2.handle);
			_sliderBox2.buttonMode = true;
			_slider2.addEventListener(Event.CHANGE, onChange);
			
			_placeSlider1 = new SliderManager();
			_placeSlider1.setUpAsstes(_amntSlider1.amountTrack, _amntSlider1.amountHandle);
			_amntSlider1.buttonMode = true;
			_placeSlider1.addEventListener(Event.CHANGE, onChange);
			
			_placeSlider2 = new SliderManager();
			_placeSlider2.setUpAsstes(_amntSlider2.amountTrack, _amntSlider2.amountHandle);
			_amntSlider2.buttonMode = true;
			_placeSlider2.addEventListener(Event.CHANGE, onChange);
			
			if(_searchResults)
			{
				_placeSlider1.percent = Number(_searchResults.maxPrice) / 1000;
				tf_maxPrice.text = _searchResults.maxPrice;
			}
			if(_searchResults)
			{
				_placeSlider2.percent = Number(_searchResults.minPrice) / 1000;
				tf_minPrice.text = _searchResults.minPrice;
			}
			
		}
		
		//onChange function looks for changes inside the sliders. When a slider changes it update the appropriate coresponding item
		private function onChange(evt:Event):void
		{
			_typePercent = ((_lb.width + 35 - (_lb.mask.width)) * _slider1.percent);
			_lb.x = -_typePercent + 33;
			
			_placeed = ((_placesLb.width + 35 - (_lb.mask.width)) * _slider2.percent);
			_placesLb.x = -_placeed + 33;
			
			_maxPrice = Math.ceil(_placeSlider1.percent * 1000);
			tf_maxPrice.text = _maxPrice.toString();
			
			_minPrice = Math.ceil(_placeSlider2.percent * 1000);
			tf_minPrice.text = _minPrice.toString();
			
			if(_minPrice >= _maxPrice - 20)
			{
				_placeSlider1.percent += .02;
			}
			
		}
		
		//create the buttonGroups for types of wines and places they were created
		private function createButtonGroup():void
		{
			
			for each( var btnName:String in _whiteWines)
			{
				var btn:WhiteBtn = new WhiteBtn();
				btn.label = btnName;
				btn.type = 'wine';
				btn.addEventListener(MouseEvent.CLICK, btnHandler);
				
				if(_searchResults && btn.label == _searchResults.type)
				{	
					btn.gotoAndStop(2);
				}else if(!_searchResults && btn.label == _type)
				{
					btn.gotoAndStop(2);	
				}
				
				_btnArray.push(btn);
				_lb.addChild(btn);
			}
			for each( var btnN:String in _places)
			{
				var btn2:WhiteBtn = new WhiteBtn();
				btn2.label = btnN;
				btn2.type = 'place';
				btn2.addEventListener(MouseEvent.CLICK, btnHandler);
				
				
				if(_searchResults && btn2.label == _searchResults.country)
				{
					btn2.gotoAndStop(2);
				}else if(!_searchResults && btn2.label == _place)
				{
					btn2.gotoAndStop(2);	
				}
				
				_btnArray.push(btn2);
				_placesLb.addChild(btn2);
			}			
		}
		
		//this function tells the buttons that only one inside each type can be down at a time
		private function btnHandler(e:MouseEvent):void
		{	
			for each(var btn:WhiteBtn in _btnArray)
			{
				if(e.currentTarget.type == btn.type)
				{
					btn.gotoAndStop(1);
				}
			}
			e.currentTarget.gotoAndStop(2);
		}
	
		//when the search button is pressed it takes the values passed in and dispatches the event for the main to listen for and respond accordingly
		private function onClick(evt:MouseEvent):void
		{	
			for each(var btn:WhiteBtn in _btnArray)
			{
				if(btn.type == 'wine' && btn.currentFrame == 2)
				{
					_type = btn.label;
				}
				if(btn.type == 'place' && btn.currentFrame == 2)
				{
					_place = btn.label;	
				}
			}
		
			_sVO = new SearchVO();
			_sVO.country = _place;
			_sVO.minPrice = _minPrice.toString();
			_sVO.maxPrice = _maxPrice.toString();
			_sVO.type = _type;
			
			var e:WineEvent = new WineEvent(WineEvent.START_SEARCH);
			e.searchVO = _sVO;
			dispatchEvent(e);
		}
	}
}